package com.cxyd.modules.wms.service.impl;

import cn.hutool.poi.excel.cell.CellSetter;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.cxyd.common.page.PageData;
import com.cxyd.common.service.impl.CrudServiceImpl;
import com.cxyd.modules.sys.dto.SysUserDTO;
import com.cxyd.modules.sys.service.SysUserService;
import com.cxyd.modules.wms.dao.ContainerDao;
import com.cxyd.modules.wms.dao.ContainerDetailDao;
import com.cxyd.modules.wms.dao.LocateDao;
import com.cxyd.modules.wms.dto.ContainerDetailDTO;
import com.cxyd.modules.wms.entity.ContainerDetailEntity;
import com.cxyd.modules.wms.entity.ContainerEntity;
import com.cxyd.modules.wms.entity.LocateEntity;
import com.cxyd.modules.wms.service.ContainerDetailService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 货格信息
 *
 * @author lp
 * @since 1.0.0 2024-08-18
 */
@Service
public class ContainerDetailServiceImpl extends CrudServiceImpl<ContainerDetailDao, ContainerDetailEntity, ContainerDetailDTO> implements ContainerDetailService {
    private final SysUserService userService;
    private final ContainerDetailDao containerDetailDao;
    private final LocateDao locateDao;
    private final ContainerDao containerDao;

    public ContainerDetailServiceImpl(SysUserService userService, ContainerDetailDao containerDetailDao, LocateDao locateDao, ContainerDao containerDao) {
        this.userService = userService;
        this.containerDetailDao = containerDetailDao;
        this.locateDao = locateDao;
        this.containerDao = containerDao;
    }

    @Override
    public PageData<ContainerDetailDTO> page(Map<String, Object> params) {
        //1.查询容器信息
        final IPage<ContainerDetailEntity> page = baseDao.selectPage(
                getPage(params, "cont_detail_id", true),
                getWrapper(params)
        );
        //2.获取用户信息
        final List<SysUserDTO> userList = userService.getList();
        //3.将creator与updater的username通过id进行绑定
        page.getRecords().forEach(entity -> {
            if (entity.getCreator() != null) {
                entity.setCreatorName(userService.getUsernameById(entity.getCreator(), userList));
            }
            if (entity.getUpdater() != null) {
                entity.setUpdaterName(userService.getUsernameById(entity.getUpdater(), userList));
            }
        });
        return getPageData(page, ContainerDetailDTO.class);
    }

    @Override
    public QueryWrapper<ContainerDetailEntity> getWrapper(Map<String, Object> params) {
        String contNo = (String) params.get("contNo");

        QueryWrapper<ContainerDetailEntity> wrapper = new QueryWrapper<>();
        wrapper.like(StringUtils.isNotBlank(contNo), "cont_no", contNo);

        return wrapper;
    }

    @Override
    public String contLocateAsk(String contNo) {

        //如果箱号在货位上就不能登记
        try {
            try
            {

                if(locateDao.selectCount(new QueryWrapper<LocateEntity>()
                        .eq("locate_type","HW")
                        .eq("cont_no",contNo))>0){
                    return "料箱在库位上未下架";
                }
            }
            catch(Exception ex){

                return baseDao.selectContLocate(contNo);

            }
            String contType = containerDao.selectOne(new QueryWrapper<ContainerEntity>()
                    .eq("cont_no",contNo)).getContType();
            if(contType.equals("1"))
                return baseDao.selectOne(new QueryWrapper<ContainerDetailEntity>()
                        .eq("cont_no",contNo)).getContLocateNo();
            else
                return baseDao.selectContLocate(contNo);
        }
        catch (Exception ex){
            return "料箱在库位上未下架或者未登记";

        }
    }
//    @Override
//    @Transactional(rollbackFor = Exception.class)
//    public void delete(@RequestBody Long[] ids) {
//        for (int i = 0; i < ids.length; i++) {
//            deleteContById(ids[i]);
//        }
//    }
//    public void deleteContById(Long id) {
//        ContainerEntity entity = containerDetailDao.getContById(id);
//        if (entity != null) {
//            throw new RootException("托盘已存在于库存中，不能删除！");
//        }
//        baseDao.deleteById(id);
//    }

}